import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import store from '../store/index.js'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Login',
    component: Login
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/index',
    name: 'index',
    children : [
      //后台界面二级路由
      {
        path: '/an',
        name: 'An',
        component: () => import(/* webpackChunkName: "index" */ '../views/an/An.vue')
      },
      {
        path: '/jiang',
        name: 'Jiang',
        component: () => import(/* webpackChunkName: "index" */ '../views/jiang/Jiang.vue')
      },
      {
        path: '/liu',
        name: 'Liu',
        component: () => import(/* webpackChunkName: "index" */ '../views/liu/Liu.vue')
      }
    ],
    component: () => import(/* webpackChunkName: "index" */ '../views/wang/index.vue')
  },
  {
    path: '/user',
    name: 'Uogin',
    component: () => import(/* webpackChunkName: "user" */ '../views/xiao/User.vue')
  },
  {
    path: '/subscribe',
    name: 'Subscribe',
    component: () => import(/* webpackChunkName: "subscribe " */ '../views/xiao/Subscribe.vue')
  },
  {
    path: '/queryvaccine',
    name: 'Queryvaccine',
    component: () => import(/* webpackChunkName: "queryvaccine " */ '../views/xiao/Queryvaccine.vue')
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
]

const router = new VueRouter({
  routes
})

// 路由守卫
// 每次路由发生变化的时候，都调用此方法
router.beforeEach((to, from, next) => {
  // alert(store.state.isLogin)
  //登录时放行
  if (to.path === "/" || to.path === "/login" || to.path === "/logout") {
    next();//放行
  } else {
    //登录成功放行
    if (store.state.isLogin) {
      next();
    } else {
      //登录失败不能访问
      next({ path: "/login" })
    }
  }

})

export default router
